<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/8 0008
 * Time: 下午 13:11
 */
namespace Home\Model;
use Think\Model;
class BCWzbmModel extends Model{
    //全文检索获取物资编码ID,并获取具体信息
    public function seach(){
        include ( "./Org/sphinx/sphinxapi.php" );
        $name = I('get.name');
        $cl = new \SphinxClient ();
        $cl->SetServer ( '119.29.92.48', 9312);
        $cl->SetConnectTimeout ( 3 );
        $cl->SetArrayResult ( true );
        $cl->SetMatchMode ( SPH_MATCH_BOOLEAN);//搜索模式
        $cl->SetLimits(0,1000);  //过滤
        $cl->SetSortMode (SPH_SORT_RELEVANCE); //排序
        $res = $cl->Query ($name,"*" );
        //dump($res);exit;
        if($wznos = array_column($res['matches'],'id')){
            $info = $this->alias('a')->field('a.wzno,a.wzmc,a.dhbs,b.flmc,c.qc')
                ->join('left join b_c_wzfl b on a.wzfl=b.flno')
                ->join('left join b_p_gysjbqk c on a.gysno=c.gysno')
                ->where(array(
                    'a.wzno' => array('in',$wznos)
                ))->select();
            $b_c_zdysx = D('b_c_zdysx a');
            $data = $b_c_zdysx->field('a.sxno, group_concat(a.zdysxz) sxz,b.sxdw,b.sxmc')
                ->join('left join b_c_sxgz b on a.sxno=b.sxno')
                ->where(array(
                    'a.wzno' => array('in',$wznos)
                ))->group('a.sxno')->select();
            foreach($data as $k=>&$v){
                $arr = explode(',',$v['sxz']);
                $v['sxz'] = array_unique($arr);
            }
            return(array(
                'sx' => $data,
                'info' =>$info
            ));
        }else{
            return false;
        }
    }

    /**获取物资编码详情
     * @return mixed
     */
    public function xq($wzno){
        $info = $this->alias('a')->field('a.wzno,a.wzmc,a.xh,a.xhgg,a.zgf,b.flno,b.flmc,c.jldwmc,d.gysno,d.qc ghs,e.zldjmc')
            ->join('left join b_c_wzfl b on a.wzfl=b.flno')
            ->join('left join b_jldw c on a.jldw=c.jldwno')
            ->join('left join b_p_gysjbqk d on a.gysno=d.gysno')
            ->join('left join b_zldjjbxx e on a.zldj=e.zldjno')
            ->find($wzno);
        $b_c_zdysx = D('b_c_zdysx a');
        $info['sx'] = $b_c_zdysx->field('b.sxmc,b.sxdw,a.zdysxz')
            ->join('left join b_c_sxgz b on a.sxno=b.sxno')
            ->where(array(
                'wzno' => array('eq',$wzno)
            ))->select();
        return $info;
    }

    /**物资编码比对
     * @param $wznos  需要对比物资编码ID集
     * @return mixed
     */
    public function bdwz($wznos){
        $info = $this->alias('a')->field('a.wzno,a.wzmc,a.xh,a.gcjk,a.xhgg,a.zgf,b.flno,b.flmc,c.jldwmc,d.gysno,d.qc ghs,e.zldjmc')
            ->join('left join b_c_wzfl b on a.wzfl=b.flno')
            ->join('left join b_jldw c on a.jldw=c.jldwno')
            ->join('left join b_p_gysjbqk d on a.gysno=d.gysno')
            ->join('left join b_zldjjbxx e on a.zldj=e.zldjno')
            ->where(array(
                'wzno' => array('in',$wznos)
            ))->select();
        $b_c_zdysx = D('b_c_zdysx a');
        $sx = $b_c_zdysx->field('b.sxmc,b.sxdw,a.wzno,a.zdysxz')
            ->join('left join b_c_sxgz b on a.sxno=b.sxno')
            ->where(array(
                'wzno' => array('in',$wznos)
            ))->select();
        foreach($info as $k=>$v){
            foreach($sx as $k1=>$v1){
                if($v['wzno'] == $v1['wzno']){
                    $info[$k]['sx'][] = $v1;
                }
            }
        }
        return  $info;
    }

    /**获取供应商信息
     * @param $gysno  供应商id
     * @return mixed
     */
    public function gysinfo($gysno){
        $b_p_gysjbqk = D('b_p_gysjbqk a');
        $info = $b_p_gysjbqk->field('a.qc,a.qtmc,a.gk,a.gcdz,a.zcdz,a.dzyx,a.ssbm,a.frxm,a.yzbm,a.zcdz,a.dh,a.cz,a.zlbmfzrxm,a.zlbmfzrsj,a.zlbmfzrdzyx,a.xsbmfzrxm,a.xsbmfzrsj,a.xsbmfzrdzyx,a.lxrxm,a.lxrsj,a.lxrdzyx,b.qyxzmc')
            ->join('left join b_qyxz b on a.qyxzno=b.qyxzno')
            ->find($gysno);
        $b_p_gyszzzs = D('b_p_gyszzzs');
        $info['qyzz'] = $b_p_gyszzzs->field('gysno,rzmc,cpfw,yxq,fzjg,rzlx')->where("gysno=$gysno")->select();
        $data = $this->alias('a')->field('a.wzno,a.wzmc,a.dhbs,b.flmc')
            ->join('left join b_c_wzfl b on a.wzfl=b.flno')
            ->where(array(
                'a.gysno' => array('eq',$gysno)
            ))->limit(5)->select();
        foreach($data as $k=>&$v){
            $v['gg'] = explode(' ',$v['dhbs']);
        }
        $info['cp'] = $data;
        return $info;
    }

    /**获取(分类 or 产品系列)下的物资编码信息
     * @param $value  分类IDor系列ID
     * @param $type  类型(分类,系列)
     * @param int $length 长度
     * @return array
     */
    public function getcatwzbm($value,$type,$length=50){
        $tiaoshu = I('post.tiaoshu');
        if($tiaoshu){
            $offset = $tiaoshu;        //计算偏移量
            $tiaoshu = $tiaoshu+$length; //计算已取条数
        }else{
            $offset = 0;
            $tiaoshu = $length;
        }
        if($type == 'wzfl'){
            $sql = "select a.wzno,a.wzmc,a.dhbs,b.qc from b_c_wzbm a
                    left join b_p_gysjbqk b on a.gysno=b.gysno
                    where a.wzfl={$value} limit $offset,$length";
        }else if($type == 'cpxl'){
            $sql = "select a.wzno,a.wzmc,a.dhbs,c.qc,d.flmc from b_c_wzbm a
                    left join b_p_cpxlwzbm b on a.wzno=b.wzno
                    left join b_p_gysjbqk c on a.gysno=c.gysno
                    left join b_c_wzfl d on a.wzfl=d.flno
                    where b.cpxlno={$value} limit $offset,$length";
        }
        //dump($sql);exit;
        $info = D()->query($sql);
        //dump($info);exit;
        foreach($info as $k=>&$v){
            $type = explode(' ',$v['dhbs']);
            $v['dhbs'] = array($type[0],$type[1],$type[2]);

        }
        return array(
            'info' => $info,
            'tiaoshu' => $tiaoshu
        );
    }



}
